package ua.at.tsvetkov.webservices.parsers;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import ua.at.tsvetkov.webservices.HttpClientRequests;

/**
 * The base class for retrieve a data from web services for parsers. Used HttpClient for requests.
 * 
 * @author Alexandr Tsvetkov
 */
public class HttpClientWebParsersBase extends HttpClientRequests{

	/**
	 * Get InputStream from Internet by url.
	 * 
	 * @param nameValuePairs
	 * @param urlStr
	 * @return InputStream
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	protected static InputStream getWebInputStream(String request, List<BasicNameValuePair> nameValuePairs) throws IllegalStateException, IOException {
		String responce = HttpClientRequests.sendPostRequest(request, nameValuePairs);
		if (responce == null)
			return null;
		else
			return new ByteArrayInputStream(responce.getBytes("UTF-8"));
	}

	/**
	 * Get InputStream from Internet by file name.
	 * 
	 * @param pathAndFileName
	 * @return InputStream
	 * @throws FileNotFoundException
	 */
	protected static InputStream getFileInputStream(String pathAndFileName) throws FileNotFoundException {
		File file = new File(pathAndFileName);
		InputStream in = new FileInputStream(file);
		return in;
	}

}
